#include "smallenemy.h"

SmallEnemy::SmallEnemy(QGraphicsScene *m_scene)
{
    root = QFileInfo(__FILE__).absolutePath();
    setVisible(false);//设置不可见
    current_blood = 1000.0f;
    current_blood_front_x = 49.0f;
    blood_front_x = 49.0f;
    isshowblood = 0;
    speed = 1;

    Rotate = 0.0f;

    blood = 1000.0f;
    blood_gb = new QPixmap(root + "/res/doom_slider_back.png");
    *blood_gb = blood_gb->scaled(51,8);
    blood_front = new QPixmap(root + "/res/doom_slider_front.png");
    *blood_front = blood_front->scaled(current_blood_front_x,6);
    state = 0;
    stopTime = 0;

    for(int i=0;i<9;i++){
        pix.append(QPixmap(root+"/res/small_moving_frame_0"+QString::number(i+1)+".png"));
    }
    for(int i=0;i<7;i++){
        pix.append(QPixmap(root+"/res/small_moving_frame_"+QString::number(i+10)+".png"));
    }

    scene = m_scene;
    scene->addItem(this);




}
QRectF SmallEnemy:: boundingRect() const{
    return QRectF(-pix[0].width()*0.5,-pix[0].height()*0.5,pix[0].width(),pix[0].height()-10);
}
void SmallEnemy:: paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

    if(isshowblood == 1){
        painter->drawPixmap(-blood_gb->width()*0.5,-pix[0].height()*0.5-10,*blood_gb);
        painter->drawPixmap(-blood_gb->width()*0.5+1,-pix[0].height()*0.5-9,blood_front->scaled(current_blood_front_x,6));
    }
    if(Rotate<-90&&Rotate>-270){
        painter->drawPixmap(-pix[state].width()*0.5,-pix[state].height()*0.5,pix[state]);
        //painter->drawPixmap(-pix[state+8].width()*0.5,-pix[state+8].height()*0.5,pix[state+8]);
    }
    else{
        painter->drawPixmap(-pix[state+8].width()*0.5,-pix[state+8].height()*0.5,pix[state+8]);
        //painter->drawPixmap(-pix[state].width()*0.5,-pix[state].height()*0.5,pix[state]);
    }
    if(stopTime%10==0){
        state = (state+1)%8;
    }
}
void SmallEnemy::advance(int phase){
    stopTime = (stopTime+1)%20;
    if(stopTime%10==0){
        if(isVisible()){
            if(mapToScene(0,0).x()<-150||mapToScene(0,0).y()<-150||mapToScene(0,0).x()>1750||mapToScene(0,0).y()>1050){
                setVisible(false);//设为不可见
            }else{
                if(Rotate<-90||Rotate>90){
                    setPos(mapToScene(-speed,0));
                }else{
                    setPos(mapToScene(speed,0));
                }
            }
        }
    }
}
void SmallEnemy:: setPos(const QPointF &pos){
    QGraphicsItem::setPos(pos);
}
void SmallEnemy::setVisible(bool visible){
    QGraphicsItem::setVisible(visible);
}

void SmallEnemy::setRotation(float angle)
{
    Rotate = angle;
    if(Rotate<-90||Rotate>90){
        QGraphicsItem::setRotation(angle+180);
    }else{
        QGraphicsItem::setRotation(angle);
    }

}

bool SmallEnemy::isVisible()
{
    return QGraphicsItem::isVisible();
}

void SmallEnemy::setBlood(float damage)
{
    current_blood -= damage;
    current_blood_front_x -= blood_front_x * (damage/blood);
    isshowblood = 1;
    update();
}
